import TalkingData from './TalkingData.js';

export default {
  methods: {
    // 加入CNZZ埋点--h5
    setCNZZScript() {
      const head = document.getElementsByTagName('head')[0];
      const scripts = document.getElementsByTagName('script');
      for (let i = 0; i < scripts.length; i++) {
        if (scripts[i].getAttribute('cnzz-flag')) {
          return false;
        }
      }
      const script = document.createElement('script');
      script.style.display = 'none';
      if (process.env.VUE_NODE_ENV === 'production') {
        script.src = 'https://s9.cnzz.com/z_stat.php?id=1279629524&web_id=1279629524';
      } else {
        script.src = 'https://s9.cnzz.com/z_stat.php?id=1279405346&web_id=1279405346';
      }

      script.setAttribute('type', 'text/javascript');
      script.setAttribute('cnzz-flag', true);

      head.appendChild(script);
    },
    // 加入TalkingData Tracking埋点--h5
    setTDh5Tracking() {
      const head = document.getElementsByTagName('head')[0];
      const scripts = document.getElementsByTagName('script');
      for (let i = 0; i < scripts.length; i++) {
        if (scripts[i].getAttribute('td-h5-flag')) {
          return false;
        }
      }
      const script = document.createElement('script');
      // console.log('tracking', process.env.VUE_NODE_ENV);
      if (process.env.VUE_NODE_ENV === 'production') {
        script.src = '/dz-mall/otc/front/lib/td-h5-website-sdk.js';
        script.setAttribute('td-appid', '5008DB2B942D44849330344846698098');
      } else {
        script.src = '/dz-mall/otc/front/lib/td-h5-website-sdk-test.js';
        script.setAttribute('td-appid', 'E982A2566F2044938029FD3675C39CF9');
      }

      script.setAttribute('type', 'text/javascript');
      script.setAttribute('td-h5-flag', true);

      head.appendChild(script);
    },
    buryPagePoint(pageName) {
      const ua = window.navigator.userAgent;
      if (ua.indexOf('thinkive') > -1) {
        TalkingData.onPage(pageName);
      }
    },
    buryEventPoint(eventName, btnName, options) {
      const ua = window.navigator.userAgent;
      if (ua.indexOf('thinkive') > -1) {
        TalkingData.onEventWithParameters(eventName, btnName, options);
      } else {
        window.TDAPP && window.TDAPP.onEvent(eventName, btnName, options);
      }
    },
    // category 必填，表示事件发生在谁身上，如“视频”、“小说”、“轮显层”等等
    // action 必填，表示访客跟元素交互的行为动作，如“播放”、“收藏”、“翻层”等等
    // label 非必填，用于更详细的描述事件
    // value 非必填，用于填写打分型事件的分值
    // nodeid 非必填，填写事件元素的div元素id
    buryEventPointCNZZ(category, action, label, value, nodeid) {
      if (window._czc && window._czc.push) {
        window._czc.push(['_trackEvent', category, action, label, value, nodeid]);
      }
    }
  }
};
